#!/bin/bash

. guk_common.h

usage() {
    cat << EOF
USAGE: $0 [OPTIONS] url

url must not contain "http://" prefix, and it must be exactly the
same as it is written in the http packet.

OPTIONS:
   -a <abstract_path> : server's abstract unix domain socket path.
   -U <unix_path>     : server's unix domain socket path.
EOF
}

test_url() {
    hit=0
    recv=0
    while read seq result; do
	recv=$(($recv | $seq))
	if [ "$result" -le 2 ]; then
	    hit=$(($hit | $result))
	else
	    echo -n "Experienced error when testing for GFW type$seq: "
	    case $result in
		"$GUK_QUERY_FORMAT_ERROR")
		echo "url too long."
		;;
		"$GUK_RESULT_SERVER_FAIL")
		echo "server failure."
		;;
	    esac
	fi
	if [ $recv -eq 3 ]; then break; fi
    done < <(echo -en "1 1 $1\n2 2 $1\n" | socat -t 20 "$SOCK_ADDR" -)
    pkill -P $!
    if [ $(($recv & 1)) -eq 0 ]; then
	echo "Experienced error when testing for GFW type1: server did not response."
    fi
    if [ $(($recv & 2)) -eq 0 ]; then
	echo "Experienced error when testing for GFW type2: server did not response."
    fi

    echo -n "$1 "
    case $hit in
	0)
	echo "does not hit GFW."
	;;
	1)
	echo "hit GFW type1."
	;;
	2)
	echo "hit GFW type2."
	;;
	3)
	echo "hit both GFW type1 and type2."
	;;
    esac
} 

ABSTRACT=$GUK_ABSTRACT_SERV_PATH
UNIX=

while getopts "a:U:" OPTION; do
    case "$OPTION" in
	h|?)
	    usage
	    exit 1
	    ;;
	a)
	    ABSTRACT=$OPTARG
	    UNIX=
	    ;;
	U)
	    if [ -z $OPTARG ]; then
		UNIX=$GUK_UNIX_SERV_PATH
	    else
		UNIX=$OPTARG
	    fi
	    ABSTRACT=
	    ;;
    esac
done

if [ -z $ABSTRACT ]; then
    SOCK_ADDR="unix-connect:$UNIX"
else
    SOCK_ADDR="abstract-connect:$ABSTRACT"
fi

if [ "x$*" != x ]; then
    test_url $*
    exit 0
fi

while read URL; do
    test_url $URL
    echo
done
